# coding:utf-8
# @Time    : 2021/12/10 8:54
# @Author  : Martin

"""
线下单品赠品
"""
from page_object.base import PageItems
from util.read_file import get_approve_data


class SingleGiftPage(PageItems):
    _base_url = "/nspas-web-in/processRoleFilter.do?businessTemplateId=NSOA23406&comType=1"

    def input_act_rule(self, params):
        """
        免费赠品
        :param params:
        :return:
        """
        for param in params:
            index1 = params.index(param) + 1
            # 输入门槛金额
            self.input('css selector',
                       f'#detailinfoCommodityPresentRule>table:nth-child({index1}) input[name="privilegeRule'
                       f'.privilegesill"]', param['limit'])
            # 点击添加赠品
            self.click('css selector',
                       f'#detailinfoCommodityPresentRule>table:nth-child({index1}) a[onclick="tradeScopeSet('
                       f'this)"]')
            # 输入可选型号数量
            self.input('id', f'promotionGoods{index1}', param['option'])
            for gift in param['gifts']:
                index2 = param['gifts'].index(gift) + 1
                # 点击增加
                self.click('xpath', f'//a[@onclick="presentScopeAdd(\'{index1}\')"]')
                # 选择赠品类型
                self.select('css selector',
                            f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                            f'select[name="activityPresentScope.giftType"]', gift['gift_type'])
                # 输入商品编码
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.commodityCode"]', gift['code'])
                # 输入供应商编码
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.supplierCode"]', gift['supplier'])

                # 输入单次赠送数量
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.singleAmount"]', gift['count'])

                # 免费赠品需要输入 地点编码和宣传价
                if gift['gift_type'] == '0':
                    # 输入地点编码
                    self.input('css selector',
                               f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                               f'input[name="activityPresentScope.plantCode"]', gift['plant'])
                    # 输入宣传价
                    self.input('css selector',
                               f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                               f'input[name="activityPresentScope.publicityPrice"]', gift['pub_price'])
                # 商转赠需要输入赠送总数量
                if gift['gift_type'] == '1':
                    # 输入赠送总数量
                    self.input('css selector',
                               f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                               f'input[name="activityPresentScope.totalAmount"]', gift['total'])
                # 输入销售价
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.salePrice"]', gift['sale_price'])

            # 点击确定
            self.click('xpath', f'//a[@onclick="tradeScopeOk(\'{index1}\')"]')
            if index1 != len(params):
                # 点击增加层级
                self.click('xpath', '//a[@onclick="commodityPresentAdd()"]')

    @staticmethod
    def get_approve_head_create():
        """
        获取总部新建审批流账号数据
        :return:
        """
        return get_approve_data('offline_single_gift', 'head', 'create')

    def add_single(self, params):
        """
        添加单品包含
        :param params:商品范围列表
        :return:
        """
        self._select_relation('包含')
        for param in params:
            self._input_commdty_scope(**param)
            self.click('id', 'cmmdCodeAdd')
        self.click('link text', '添加到商品范围')